import os
import shutil
import common.gyblog as log

LOG = log.get_logger()
PATH = r'\\lcnas\xishu16T_2022\07-FC2\\'
PATH_TEMP = r'\\lcnas\xishu16T_2022\07-FC2temp\\'


# 删除目录和目录下所有文件
def delete_folder_contents(folder):
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            print(f'Failed to delete {file_path}. Reason: {e}')
    os.rmdir(folder)


def manage_fc2_dir(path):
    root_dirs = os.listdir(path)
    for root_dir in root_dirs:
        dirs = os.listdir(path + root_dir)
        # 如果是空目录，直接删除
        if len(dirs) == 0:
            os.rmdir(path + root_dir)
            LOG.info('删除空文件夹：' + path + root_dir)
            print('删除空文件夹：' + path + root_dir)
        else:
            jpg_count = 0
            mp4_count = 0
            nfo_count = 0
            for dir in dirs:
                houzui = dir.split('.')[-1]
                if houzui == 'jpg' or houzui == 'png':
                    jpg_count = jpg_count + 1
                if houzui == 'mp4':
                    mp4_count = mp4_count + 1
                if houzui == 'nfo':
                    nfo_count = nfo_count + 1
            # 如果没有mp4  直接删除
            if mp4_count == 0:
                delete_folder_contents(path + root_dir)
                LOG.info('删除没有mp4的文件夹：' + path + root_dir)
                print('删除没有mp4的文件夹：' + path + root_dir)
                continue
            # 如果没有nfo 提示
            if nfo_count == 0:
                print('【ERROR】没有nfo：' + path + root_dir)
            elif nfo_count < mp4_count:
                print('缺少nfo：' + path + root_dir + 'nfo数量：{}，视频数量：{}'.format(str(nfo_count), str(mp4_count)))
                LOG.error(
                    '缺少nfo：' + path + root_dir + 'nfo数量：{}，视频数量：{}'.format(str(nfo_count), str(mp4_count)))
            # 如果没有图片 提示
            if jpg_count == 0:
                print('【ERROR】没有图片：' + path + root_dir)
            elif jpg_count / 3 != mp4_count:
                # print('缺少图片：' + path + root_dir + '图片数量：{}，视频数量：{}'.format(str(jpg_count), str(mp4_count)))
                LOG.error(
                    '缺少图片：' + path + root_dir + '图片数量：{}，视频数量：{}'.format(str(jpg_count), str(mp4_count)))


def move_mp4s_to_temp(path, path_temp):
    root_dirs = os.listdir(path)
    for root_dir in root_dirs:
        dirs = os.listdir(path + root_dir)
        for dir in dirs:
            houzui = dir.split('.')[-1]
            if houzui == 'mp4':
                source_file = path + root_dir + '\\' + dir
                target_file = path_temp + dir
                print('移动：{}，{}'.format(source_file, target_file))
                shutil.move(source_file, target_file)


def move_mp4s_back(path_temp, path):
    root_dirs = os.listdir(path_temp)
    for root_dir in root_dirs:
        houzui = root_dir.split('.')[-1]
        front_list = root_dir.split('.')[0].split('-')
        front = '-'.join(front_list[:2])

        if houzui == 'mp4':
            source_file = path_temp + root_dir
            target_file = path + front + '\\' + root_dir
            try:
                shutil.move(source_file, target_file)
                print('移动：{}，{}'.format(source_file, target_file))
            except:
                print('移动：{}，{}失败！！！！！'.format(source_file, target_file))
                LOG.error('移动：{}，{}失败！！！！！'.format(source_file, target_file))


if __name__ == '__main__':
    '''
    1：管理FC2, 删除没有MP4的文件夹
    2：移出
    3：移回
    '''
    mode = 1
    if mode == 1:
        manage_fc2_dir(PATH)
    if mode == 2:
        move_mp4s_to_temp(PATH, PATH_TEMP)
    if mode == 3:
        move_mp4s_back(PATH_TEMP, PATH)
